<template>
  <div class="app-container">
      <SksTableQueryFormEl :showDeleteFilter="false" :mainTable="mainTable" :queryParams="mainTable.queryParams" :showSearch="mainTable.showSearch" :queryParamsColumnList="mainTable.queryParamsColumnList" @handleQuery="handleQuery" @resetQuery="resetQuery">
          <template slot="default" slot-scope="{queryParams}">
          </template>
      </SksTableQueryFormEl>

      <SksTable :main-table="mainTable" :paginationPageSizes="[5,10, 20, 30, 50]" @refresh="getMainList()">
          <template slot="top">
              <el-col :span="1.5">
                  <el-button
                      type="primary"
                      plain
                      icon="el-icon-plus"
                      size="mini"
                      @click="handleAdd"
                      v-hasPermi="['system:dict:add']"
                  >新增</el-button>
              </el-col>
              <el-col :span="1.5">
                  <el-button
                      type="success"
                      plain
                      icon="el-icon-edit"
                      size="mini"
                      :disabled="!sksUtils.safeGet(mainTable,`currentRow.${mainTable.uniqueId}`)"
                      @click="handleUpdate(mainTable.currentRow)"
                      v-hasPermi="['system:dict:edit']"
                  >修改</el-button>
              </el-col>
              <el-col :span="1.5">
                  <el-button
                      type="danger"
                      plain
                      icon="el-icon-delete"
                      size="mini"
                      :disabled="mainTable.multiple"
                      @click="handleDelete"
                      v-hasPermi="['system:dict:remove']"
                  >删除</el-button>
              </el-col>
              <el-col :span="1.5">
                  <el-button
                      type="warning"
                      plain
                      icon="el-icon-download"
                      size="mini"
                      @click="handleExport"
                      v-hasPermi="['system:dict:export']"
                  >导出</el-button>
              </el-col>
              <el-col :span="1.5">
                  <el-button
                      type="danger"
                      plain
                      icon="el-icon-refresh"
                      size="mini"
                      @click="handleRefreshCache"
                      v-hasPermi="['system:dict:remove']"
                  >刷新缓存</el-button>
              </el-col>
          </template>
          <template slot="default">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="字典编号" align="center" prop="dictId" />
              <el-table-column label="字典名称" align="center" prop="dictName" :show-overflow-tooltip="true" />
              <el-table-column label="字典类型" align="center" :show-overflow-tooltip="true">
                  <template slot-scope="scope">
                      <router-link :to="'/system/dict-data/index/' + scope.row.dictId" class="link-type">
                          <span>{{ scope.row.dictType }}</span>
                      </router-link>
                  </template>
              </el-table-column>
              <el-table-column label="状态" align="center" prop="status">
                  <template slot-scope="scope">
                      <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>
                  </template>
              </el-table-column>
              <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />
              <el-table-column label="创建时间" align="center" prop="createTime" width="180">
                  <template slot-scope="scope">
                      <span>{{ parseTime(scope.row.createTime) }}</span>
                  </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                  <template slot-scope="scope">
                      <el-button
                          size="mini"
                          type="text"
                          icon="el-icon-edit"
                          @click="handleUpdate(scope.row)"
                          v-hasPermi="['system:dict:edit']"
                      >修改</el-button>
                      <el-button
                          size="mini"
                          type="text"
                          icon="el-icon-delete"
                          @click="handleDelete(scope.row)"
                          v-hasPermi="['system:dict:remove']"
                      >删除</el-button>
                  </template>
              </el-table-column>
          </template>
      </SksTable>
<!--    <el-table v-loading="loading" :data="typeList" @selection-change="handleSelectionChange">-->
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="字典编号" align="center" prop="dictId" />-->
<!--      <el-table-column label="字典名称" align="center" prop="dictName" :show-overflow-tooltip="true" />-->
<!--      <el-table-column label="字典类型" align="center" :show-overflow-tooltip="true">-->
<!--        <template slot-scope="scope">-->
<!--          <router-link :to="'/system/dict-data/index/' + scope.row.dictId" class="link-type">-->
<!--            <span>{{ scope.row.dictType }}</span>-->
<!--          </router-link>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="状态" align="center" prop="status">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.sys_normal_disable" :value="scope.row.status"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="备注" align="center" prop="remark" :show-overflow-tooltip="true" />-->
<!--      <el-table-column label="创建时间" align="center" prop="createTime" width="180">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{ parseTime(scope.row.createTime) }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
<!--        <template slot-scope="scope">-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-edit"-->
<!--            @click="handleUpdate(scope.row)"-->
<!--            v-hasPermi="['system:dict:edit']"-->
<!--          >修改</el-button>-->
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['system:dict:remove']"-->
<!--          >删除</el-button>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--    </el-table>-->

<!--    <pagination-->
<!--      v-show="total>0"-->
<!--      :total="total"-->
<!--      :page.sync="queryParams.pageNum"-->
<!--      :limit.sync="queryParams.pageSize"-->
<!--      @pagination="getList"-->
<!--    />-->


      <AddDictFormDialog ref="addDictFormDialogRef" @close="getMainList()"/>
  </div>
</template>

<script>
import { listType, getType, delType, addType, updateType, refreshCache } from "/src/api/system/dict/type";
import {sksPageMixin} from "sks-plugin-el-erp/lib/sks-page";
import AddDictFormDialog from "./AddDictFormDialog";
import {sksParamColumn,sksQueryConfigMapToList} from "sks-plugin-el-erp/lib/sks-app-utils";
import {getDicts, getMultipleDicts} from "../../../api/system/dict/data";
import {deepCopy} from "sks-plugin-el-erp/lib/core";


export default {
  name: "Dict",
    components: {AddDictFormDialog},
    mixins: [
        sksPageMixin,
    ],
  dicts: ['sys_normal_disable'],
  data() {
    return {
        sksQueryConfigMap:{
            dictNameLike: sksParamColumn({
                htmlType: `input`,
                //根据map中的key为基准
                // prop: `nameLike`,
                label: '字典名称',
            }),
            dictTypeLike: sksParamColumn({
                htmlType: `input`,
                //根据map中的key为基准
                // prop: `nameLike`,
                label: '字典类型',
            }),
            createTimeBetween: sksParamColumn({
                htmlType: `sksFilterParamDateRange`,
                //根据map中的key为基准
                // prop: `nameLike`,
                label: '字典类型',
            }),
        },

        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 字典表格数据
      typeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dictName: undefined,
        dictType: undefined,
        status: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        dictName: [
          { required: true, message: "字典名称不能为空", trigger: "blur" }
        ],
        dictType: [
          { required: true, message: "字典类型不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
  },
  methods: {
      initMainTableParam(){
          return {
              //业务名称,通常用业务表名
              ownerName: `sys_dict`,
              uniqueId: 'dictId',

              //应用名称,根据改名称查询列配置信息
              sksAppName: 'dictPage',

              showSearchButton: true,
              //使用 tableColumnList 渲染
              tableColumnListEnable: false,
              //表列 会根据 sksAppName 自动查询后台配置
              tableColumnList: [],

              queryParamsColumnListEnable: false,
              //搜索字段 会根据 sksAppName 自动查询后台配置
              queryParamsColumnList: sksQueryConfigMapToList(this.sksQueryConfigMap),

              //通过 appName 查找显示列表配置信息
              sksAppNameToJumpEditSksAppConfig: true,
              queryParams:{
                  pageSize:5,
              }
          }
      },
      sksInitAfter(){
          this.mainTable.queryParams.pageSize=5
          getMultipleDicts([{
                  //字典类型
                  dictType:'sys_normal_disable',
                  //用于返回map中的 key 名称
                  // queryParams.sksParams 中下拉选项通常使用 prop作为 aliasName,例如"statusEquals"
                  // 如果为空则按照 r1,r2,r3命名
                  aliasName: 'statusEquals',
                  //是否在第一个元素中插入'全部选项'
                  unshiftAllOption: true,
          }]).then(res=>{
              let tmp=deepCopy(this.sksQueryConfigMap)
              let selectOptions=res.data.statusEquals
              tmp.statusEquals= sksParamColumn({
                  htmlType: `dictFormat`,
                  //根据map中的key为基准
                  // prop: `nameLike`,
                  label: '状态',
                  selectOptions: selectOptions,
              })
              this.mainTable.queryParamsColumnList= sksQueryConfigMapToList(tmp)
          })
      },
      getMainList(){
          let queryParams=this.addDateRange(this.mainTable.queryParams, this.dateRange)
          this.sksUtils.resetSksTableBefore(this.mainTable)
          listType(queryParams).then(res=>{
              this.sksUtils.setSksTableDataByPage(this.mainTable,res.data)
          })
      },
    /** 新增按钮操作 */
    handleAdd() {
        this.$refs.addDictFormDialogRef.show({
            form:{
                status: '0',
                virtualChangeBitFlag: this.sksUtils.CHANGE_BIT_FLAG.add,
            },
            dialogInfo:{
                title: '新增字典类型',
            }
        })
      //   this.reset();
      //   this.open = true;
      // this.title = "添加字典类型";
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.dictId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
        const dictId = row.dictId || this.mainTable.currentRow[this.mainTable.uniqueId]
        this.$refs.addDictFormDialogRef.showByUniqueId({
            uniqueId: dictId,
            virtualChangeBitFlag: this.sksUtils.CHANGE_BIT_FLAG.modify,
            dialogInfo:{
                title: '修改字典类型',
            }
        })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const dictIds = row.dictId || this.mainTable.ids;
      this.$modal.confirm('是否确认删除字典编号为"' + dictIds + '"的数据项？').then(function() {
        return delType(dictIds);
      }).then(() => {
        this.getMainList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dict/type/export', {
        ...this.queryParams
      }, `type_${new Date().getTime()}.xlsx`)
    },
    /** 刷新缓存按钮操作 */
    handleRefreshCache() {
      refreshCache().then(() => {
        this.$modal.msgSuccess("刷新成功");
        this.$store.dispatch('dict/cleanDict');
      });
    }
  }
};
</script>
